﻿package{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import gs.TweenMax;
	import gs.easing.*;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	public class Main extends Sprite
	{
		private var viewPort:Viewport3D;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var renderer:BasicRenderEngine;
		private var dispObj3D:DisplayObject3D;
		private var totalMenuNum:Number;
		private var myAngle:Number;
		private var radian:Number;
		private var selNum:Number;
		private var matArr:Array;
		
		public function Main():void
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function init(_e:Event=null):void
		{
			setVars();
			init3D();
			createObj();
			setBtns();
			renderFn();
			firstMove();
			unFold();
			addEventListener(Event.ENTER_FRAME,renderFn);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
		}
		private function setVars():void
		{
			totalMenuNum = 11;
			myAngle = 360/totalMenuNum;
			radian = 300;
			selNum = 0;
			matArr = new Array();
		}
		
		/**
		 * ViewPort, Scene, Camera, Renderer, displayObj3D  설정
		 * 
		 */
		private function init3D():void
		{
			viewPort 	= new Viewport3D(550,400);	
			scene 		= new Scene3D();
			camera 		= new Camera3D();
			camera.y 	= 100;
			renderer 	= new BasicRenderEngine();
			
			dispObj3D = new DisplayObject3D('Start');
			dispObj3D.rotationY = -360;
			dispObj3D.z = 500;
			
			addChild(viewPort);
			scene.addChild(dispObj3D);
		}
		
		/**
		 * 각 오브젝트 세팅 
		 * 
		 */		
		private function createObj():void
		{
			for(var i:Number=0;i<totalMenuNum;i++){
				
				/* mc 생성 */
				var mc:MovieClip = new Obj();
				//mc.txt.text = i;
				
				/* Material 생성 */
				var material:MovieMaterial = new MovieMaterial(mc);
				material.doubleSided=true;
				material.interactive = true;
				material.smooth = true;
				material.movie.addEventListener(MouseEvent.MOUSE_DOWN,click);
				
				/* Plane 생성 */
				var plane:Plane = new Plane(material, material.movie.width, material.movie.height, 8, 8 );
				matArr.push(plane);
				dispObj3D.addChild(plane);
				
				/* Plane position */
				var tempAng:Number = myAngle*i+90;
				tempAng = (tempAng<360)? tempAng : tempAng%360 ;
				var rad:Number = tempAng * Math.PI/180;
				var point:Point = Point.polar(radian,rad);
				plane.x = point.x;
				plane.z = -point.y;
				plane.rotationY = tempAng-90;
				
				/* mc 제거 */
				mc = null;
			}
		}

		/**
		 * 버튼 부 
		 * 
		 */		
		private function click(_e:MouseEvent):void
		{
			trace(_e);
		}
		
		private function setBtns():void
		{
			var btns:MovieClip = new Btns();
			btns.y = 343;
			btns.x = 21;
			addChild(btns);
			for(var i:Number=0;i<2;i++){
				var btn:MovieClip = btns['btn'+i];
				btn.myId = i;
				btn.addEventListener(MouseEvent.CLICK,btnDown);
			}
		}
		private function btnDown(_e:MouseEvent):void
		{
			var tarNum:Number;
			if(MovieClip(_e.currentTarget).myId==0){
				moveFn(1);
			}else{
				moveFn(-1);		
			}
		}
		
		/**
		 * 모션 부 
		 * 
		 */	
		private function renderFn(_e:Event=null):void
		{
			renderer.renderScene(scene,camera,viewPort);
		}
		private function wheelHandler(_e:MouseEvent):void
		{
			moveFn(_e.delta/3);
		}		
		private function moveFn(_num:Number):void
		{
			selNum += _num;
			var tarNum:Number = 100*selNum;
			TweenMax.to(dispObj3D,1,{x:tarNum,ease:Quint.easeOut});
		}
		
		/* 초기 에니메이션 */	
		private function firstMove():void
		{
			TweenMax.to(dispObj3D,3,{z:-150,rotationY:0,ease:Quint.easeInOut});
		}
		/* 원통 펼치기 */
		private function unFold():void
		{
			var plane:Plane;
			var tarX:Number;
			var tarRo:Number;
			for(var i:Number=0;i<totalMenuNum;i++){
				plane = matArr[i];
				tarX = (i<totalMenuNum/2)?-i:(totalMenuNum-i); 
				tarX = tarX*100;
				tarRo = (plane.rotationY>180)? 360: 0;
				TweenMax.to(plane,1.5,{x:tarX,z:-radian,rotationY:tarRo,
									bezierThrough:[{x:tarX*1.3, z:plane.z+((-radian-plane.z)/2)}],delay:3,ease:Quint.easeOut});
			}
		}
	}
}
